package com.insuremonitor.system.service.impl;

import com.insuremonitor.common.core.domain.AjaxResult;
import com.insuremonitor.common.utils.DateUtils;
import com.insuremonitor.system.domain.*;
import com.insuremonitor.system.mapper.*;
import com.insuremonitor.system.service.IFrontInsureInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 保险种类信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@Service
public class FrontInsureInfoServiceImpl implements IFrontInsureInfoService 
{
    @Autowired
    private FrontInsureInfoMapper frontInsureInfoMapper;

    @Autowired
    private FrontInsureConfigDeductionMapper frontInsureConfigDeductionMapper;

    @Autowired
    private FrontInsureConfigSafeguardMapper frontInsureConfigSafeguardMapper;

    @Autowired
    private FrontInsureRateExtendMapper frontInsureRateExtendMapper ;

    @Autowired
    private FrontInsureRateGoodsMapper frontInsureRateGoodsMapper;

    @Autowired
    private FrontInsureRateKindMapper frontInsureRateKindMapper;


    /**
     * 查询保险种类信息
     * 
     * @param insureInfoId 保险种类信息主键
     * @return 保险种类信息
     */
    @Override
    public FrontInsureInfo selectFrontInsureInfoByInsureInfoId(Long insureInfoId)
    {
        FrontInsureInfo frontInsureInfo = frontInsureInfoMapper.selectFrontInsureInfoByInsureInfoId(insureInfoId);
        if(frontInsureInfo!=null && frontInsureInfo.getInsureInfoId()!=null){
            //
            FrontInsureConfigDeduction insureConfigDeduction =new FrontInsureConfigDeduction();
            insureConfigDeduction.setStatus("1");
            insureConfigDeduction.setInsureInfoId(frontInsureInfo.getInsureInfoId());
            List<FrontInsureConfigDeduction> frontInsureConfigDeductions = frontInsureConfigDeductionMapper.selectFrontInsureConfigDeductionList(insureConfigDeduction);
            if(frontInsureConfigDeductions!=null && frontInsureConfigDeductions.size()>0){
                frontInsureInfo.setInsureConfigDeductions(frontInsureConfigDeductions);
            }


            //
            FrontInsureConfigSafeguard frontInsureConfigSafeguard =new FrontInsureConfigSafeguard();
            frontInsureConfigSafeguard.setStatus("1");
            frontInsureConfigSafeguard.setInsureInfoId(frontInsureInfo.getInsureInfoId());
            List<FrontInsureConfigSafeguard> frontInsureConfigSafeguards = frontInsureConfigSafeguardMapper.selectFrontInsureConfigSafeguardList(frontInsureConfigSafeguard);
            if(frontInsureConfigSafeguards!=null && frontInsureConfigSafeguards.size()>0){
                frontInsureInfo.setInsureConfigSafeguards(frontInsureConfigSafeguards);
            }


            //
            FrontInsureRateExtend frontInsureRateExtend =new FrontInsureRateExtend();
            frontInsureRateExtend.setStatus("1");
            frontInsureRateExtend.setInsureInfoId(frontInsureInfo.getInsureInfoId());
            List<FrontInsureRateExtend> frontInsureRateExtends = frontInsureRateExtendMapper.selectFrontInsureRateExtendList(frontInsureRateExtend);
            if(frontInsureRateExtends!=null && frontInsureRateExtends.size()>0){
                frontInsureInfo.setInsureRateExtends(frontInsureRateExtends);
            }


            //
            FrontInsureRateKind frontInsureRateKind =new FrontInsureRateKind();
            frontInsureRateKind.setStatus("1");
            frontInsureRateKind.setInsureInfoId(frontInsureInfo.getInsureInfoId());
            List<FrontInsureRateKind> frontInsureRateKinds = frontInsureRateKindMapper.selectFrontInsureRateKindList(frontInsureRateKind);
            if(frontInsureRateKinds!=null && frontInsureRateKinds.size()>0){
                for(FrontInsureRateKind insureRateKind:frontInsureRateKinds){

                    //险种与货物对应
                    FrontInsureRateGoods frontInsureRateGoods =new FrontInsureRateGoods();
                    frontInsureRateGoods.setStatus("1");
                    frontInsureRateGoods.setInsureKindId(insureRateKind.getInsureKindId());
                    List<FrontInsureRateGoods> frontInsureRateGoodss = frontInsureRateGoodsMapper.selectFrontInsureRateGoodsList(frontInsureRateGoods);
                    if(frontInsureRateGoodss!=null && frontInsureRateGoodss.size()>0){
                        insureRateKind.setInsureRateGoodss(frontInsureRateGoodss);
                    }

                }
                frontInsureInfo.setInsureRateKinds(frontInsureRateKinds);
            }

        }
        return frontInsureInfo;
    }

    /**
     * 查询保险种类信息列表
     * 
     * @param frontInsureInfo 保险种类信息
     * @return 保险种类信息
     */
    @Override
    public List<FrontInsureInfo> selectFrontInsureInfoList(FrontInsureInfo frontInsureInfo)
    {
        return frontInsureInfoMapper.selectFrontInsureInfoList(frontInsureInfo);
    }

    /**
     * 新增保险种类信息
     * 
     * @param frontInsureInfo 保险种类信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFrontInsureInfo(FrontInsureInfo frontInsureInfo)
    {
        frontInsureInfo.setCreateTime(DateUtils.getNowDate());
        int result = frontInsureInfoMapper.insertFrontInsureInfo(frontInsureInfo);
        if(result>0){
            if(frontInsureInfo.getInsureConfigDeductions()!=null && frontInsureInfo.getInsureConfigDeductions().size()>0){
                List<FrontInsureConfigDeduction> insureConfigDeductions = frontInsureInfo.getInsureConfigDeductions();
                for(FrontInsureConfigDeduction insureConfigDeduction :insureConfigDeductions){
                    insureConfigDeduction.setInsureInfoId(frontInsureInfo.getInsureInfoId());
                    insureConfigDeduction.setCreateTime(new Date());
                    frontInsureConfigDeductionMapper.insertFrontInsureConfigDeduction(insureConfigDeduction);
                }
            }

            if(frontInsureInfo.getInsureConfigSafeguards()!=null && frontInsureInfo.getInsureConfigSafeguards().size()>0){
                List<FrontInsureConfigSafeguard> insureConfigSafeguards = frontInsureInfo.getInsureConfigSafeguards();
                for(FrontInsureConfigSafeguard insureConfigSafeguard  :insureConfigSafeguards){
                    insureConfigSafeguard.setInsureInfoId(frontInsureInfo.getInsureInfoId());
                    insureConfigSafeguard.setCreateTime(new Date());
                    frontInsureConfigSafeguardMapper.insertFrontInsureConfigSafeguard(insureConfigSafeguard);
                }
            }

            if(frontInsureInfo.getInsureRateExtends()!=null && frontInsureInfo.getInsureRateExtends().size()>0){
                List<FrontInsureRateExtend> insureRateExtends = frontInsureInfo.getInsureRateExtends();
                for(FrontInsureRateExtend insureRateExtend  :insureRateExtends){
                    insureRateExtend.setInsureInfoId(frontInsureInfo.getInsureInfoId());
                    insureRateExtend.setCreateTime(new Date());
                    frontInsureRateExtendMapper.insertFrontInsureRateExtend(insureRateExtend);

                }
            }



            if(frontInsureInfo.getInsureRateKinds()!=null && frontInsureInfo.getInsureRateKinds().size()>0){
                List<FrontInsureRateKind> insureRateKinds = frontInsureInfo.getInsureRateKinds();
                for(FrontInsureRateKind insureRateKind  :insureRateKinds){
                    insureRateKind.setInsureInfoId(frontInsureInfo.getInsureInfoId());
                    insureRateKind.setCreateTime(new Date());
                    frontInsureRateKindMapper.insertFrontInsureRateKind(insureRateKind);


                    if(insureRateKind.getInsureRateGoodss()!=null && insureRateKind.getInsureRateGoodss().size()>0){
                        List<FrontInsureRateGoods> insureRateGoods = insureRateKind.getInsureRateGoodss();
                        for(FrontInsureRateGoods insureRateGood  :insureRateGoods){
                            insureRateGood.setInsureKindId(insureRateKind.getInsureKindId());
                            insureRateGood.setCreateTime(new Date());
                            frontInsureRateGoodsMapper.insertFrontInsureRateGoods(insureRateGood);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 修改保险种类信息
     * 
     * @param frontInsureInfo 保险种类信息
     * @return 结果
     */
    @Override
    public int updateFrontInsureInfo(FrontInsureInfo frontInsureInfo)
    {
        frontInsureInfo.setUpdateTime(DateUtils.getNowDate());
        return frontInsureInfoMapper.updateFrontInsureInfo(frontInsureInfo);
    }

    /**
     * 批量删除保险种类信息
     * 
     * @param insureInfoIds 需要删除的保险种类信息主键
     * @return 结果
     */
    @Override
    public int deleteFrontInsureInfoByInsureInfoIds(Long[] insureInfoIds)
    {
        return frontInsureInfoMapper.deleteFrontInsureInfoByInsureInfoIds(insureInfoIds);
    }

    /**
     * 删除保险种类信息信息
     * 
     * @param insureInfoId 保险种类信息主键
     * @return 结果
     */
    @Override
    public int deleteFrontInsureInfoByInsureInfoId(Long insureInfoId)
    {
        return frontInsureInfoMapper.deleteFrontInsureInfoByInsureInfoId(insureInfoId);
    }

    @Override
    public AjaxResult enshelf(FrontInsureInfo frontInsureInfo) {
        frontInsureInfo.setUpdateTime(DateUtils.getNowDate());
        frontInsureInfo.setShelfStatus("1");
        int result= frontInsureInfoMapper.updateFrontInsureInfo(frontInsureInfo);
        if(result>0){
            return  AjaxResult.success("操作成功");
        }else {
            return  AjaxResult.error("操作失败");
        }
    }

    @Override
    public AjaxResult disshelf(FrontInsureInfo frontInsureInfo) {
        frontInsureInfo.setUpdateTime(DateUtils.getNowDate());
        frontInsureInfo.setShelfStatus("0");
        int result= frontInsureInfoMapper.updateFrontInsureInfo(frontInsureInfo);
        if(result>0){
            return  AjaxResult.success("操作成功");
        }else {
            return  AjaxResult.error("操作失败");
        }
    }
}
